
   /****************************************** 
  	 捕捉左右滑动屏幕，实时更新图表
   *******************************************/
   
   //滑动
  var touchStartX = 0;
  var touchStartY = 0;
  var firstTouchStartX;
  var firstTouchStartY;
  var boolSwipeX;
  var boolSwipeY;
  var onMoveX;
  var onMoveY;
  var swipeXLength;
  var swipeYLength;
  var preSwipeFirstXAxisDate;
  var preSwipeLastXAxisDate;
   
  function initSwipe(){	  
	  window.onload = initSwipeEventListener();
	  preSwipeFirstXAxisDate = xAxisDateArray[0];
	  preSwipeLastXAxisDate = xAxisDateArray[xAxisDateArray.length - 1];
  }
  
  //判断是否支持触摸事件
   function initSwipeEventListener() {        
	    try {
	        	document.createEvent("TouchEvent");
	        	bindSwipeEventListener(); //绑定事件
	    }
	    catch (e) {
	        //TO-LIST
	    }
    }
    
    //绑定事件
    function bindSwipeEventListener() {
        document.addEventListener('touchstart', touchSatrtFunc, false);
        document.addEventListener('touchmove', touchMoveFunc, false);
        document.addEventListener('touchend', touchEndFunc, false);
    }
  
  	
    //touchstart事件
    function touchSatrtFunc(evt) {
        try
        {
        	evt.stopPropagation();//阻止冒泡
            evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动条滚动等
			
			//记录触点初始位置
            var touch = evt.touches[0]; //获取第一个触点
            touchStartX = Number(touch.pageX); //页面触点X坐标
            touchStartY = Number(touch.pageY); //页面触点Y坐标
            
            firstTouchStartX = touchStartX;
            firstTouchStartY = touchStartY;
                       
            boolSwipeX = true;
     		boolSwipeY = true ;
        }
        catch (e) {
            //TO-LIST
        }
    }

    //touchmove事件，这个事件无法获取坐标
    function touchMoveFunc(evt) {
        try
        {
        	evt.stopPropagation();//阻止冒泡
            evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动条滚动等

            var touch = evt.touches[0]; //获取第一个触点			
            //左右滑动            
            if (isReasonableSwipe(touch)) {
            	var addMucusChartDataFromHead = false;               		            
                
                if(isSwipeRight())
                	addMucusChartDataFromHead = true; 
                
                swipeMucusChart(addMucusChartDataFromHead);               
                                
                touchStartX = onMoveX;
                boolSwipeY = false;
            }
            //上下滑动
            else if(isSwipeVertical()){  
		        boolSwipeX = false ;		          
		    }		    
        }
        catch (e) {
            //TO-LIST
        }
    }
    
    //touchend事件
    function touchEndFunc(evt) {
        try {
        		evt.stopPropagation();//阻止冒泡
            	evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动条滚动等           	            		  	
        }
        catch (e) {
            //TO-LIST
        }
    }
    
    function isReasonableSwipe(touch){      	
    	return ( isSwipeInMucusChartZone() && isSwipeHorizon(touch) );
    }
    
    function isSwipeInMucusChartZone(){
    	return (isFirstTouchStartXInMucusChartZone() && isFirstTouchStartYInMucusChartZone());
    }
    
    function isFirstTouchStartXInMucusChartZone(){
    	return ( (firstTouchStartX >= gridX) && (firstTouchStartX <= mucusChartWidth - gridX2) );
    }
    
    function isFirstTouchStartYInMucusChartZone(){
    	return ( ( firstTouchStartY >= gridY ) && ( firstTouchStartY <= (mucusChartHeight - gridY2) ) );
    }
    
    function isSwipeHorizon(touch){
		getOnMoveXY(touch);         
		getSwipeXYLength();
		return (boolSwipeX && (swipeXLength > swipeYLength));
	}
	
    function getOnMoveXY(touch){
    	onMoveX = Number(touch.pageX); //滑动时的触点X坐标
        onMoveY = Number(touch.pageY); //滑动时的触点Y坐标 
    }
    
    function getSwipeXYLength(){
    	swipeXLength = Math.abs(onMoveX - touchStartX);
		swipeYLength = Math.abs(onMoveY - touchStartY);
    }
	
	function isSwipeVertical(){
		return (boolSwipeY && (swipeYLength > swipeXLength));
	}
	
	function isSwipeLeft(){
		return onMoveX < touchStartX;
	}
	
	function isSwipeRight(){
		return onMoveX > touchStartX;
	}

  function swipeMucusChart(addMucusChartDataFromHead){ 	   
  	   var newXAxisDateData;
	   var newYAxisMucusData;	   	
	   
	   newXAxisDateData = getNewXAxisDateData(addMucusChartDataFromHead);	 	
	   newYAxisMucusData = getNewYAxisMucusData(newXAxisDateData);
	   refreshFirstLastXAxisDate(addMucusChartDataFromHead);
	   refreshSwipeMucusChartXY(addMucusChartDataFromHead, newXAxisDateData, newYAxisMucusData);	   	      
	   refreshXYAxisDateArray(addMucusChartDataFromHead, newXAxisDateData, newYAxisMucusData);		   	  
  }
  
  function getNewXAxisDateData(addMucusChartDataFromHead){
  	if(addMucusChartDataFromHead)
	   	newXAxisDateData = getYesterdayDateStr(preSwipeFirstXAxisDate);	   	  
    else
   	  	newXAxisDateData = getTomorrowDateStr(preSwipeLastXAxisDate);
   	  	
   	return newXAxisDateData;
  }    
 
  function getNewYAxisMucusData(newXAxisDateData){
  		return customerDateMucusMap.get(newXAxisDateData);
  }
  
  function refreshFirstLastXAxisDate(addMucusChartDataFromHead){
  	if(addMucusChartDataFromHead){   	  
   	  	preSwipeFirstXAxisDate = getYesterdayDateStr(preSwipeFirstXAxisDate);
   	  	preSwipeLastXAxisDate = getYesterdayDateStr(preSwipeLastXAxisDate);
    }
    else{
   	  	preSwipeFirstXAxisDate = getTomorrowDateStr(preSwipeFirstXAxisDate);
   	  	preSwipeLastXAxisDate = getTomorrowDateStr(preSwipeLastXAxisDate);	   	  
    }
  }
    
  function refreshXYAxisDateArray(addMucusChartDataFromHead, newXAxisDateData, newYAxisMucusData){
	  	if(addMucusChartDataFromHead){
	  	    xAxisDateArray.pop();
            mucusDataArray.pop();
            xAxisDateArray.unshift(newXAxisDateData);
            mucusDataArray.unshift(newYAxisMucusData);
	  	}
	  	else{
	  		xAxisDateArray.shift();
            mucusDataArray.shift();
            xAxisDateArray.push(newXAxisDateData);
            mucusDataArray.push(newYAxisMucusData);
	  	}
  }

  function refreshSwipeMucusChartXY(addMucusChartDataFromHead, newXAxisDateData, newYAxisMucusData){
  		mucusChart.addData([  							
  							[
	  							0,
	  							newYAxisMucusData,
	  							addMucusChartDataFromHead,
	  							false,
	  							newXAxisDateData
	  						] 		 		
  		]);   			
  }
  